package com.northpool.operator.dataset.index;

import com.northpool.operator.annotation.InputParam;
import com.northpool.operator.annotation.InputParams;
import com.northpool.operator.annotation.OperatorInfo;
import com.northpool.operator.annotation.OutputParam;
import com.northpool.operator.annotation.OutputParams;
import com.northpool.operator.common.algorithm.OperatorTestBuild;
import com.northpool.operator.common.algorithm.TestParamBuilder;
import com.northpool.operator.common.database.DatabaseUtils;
import com.northpool.operator.param.ParamTool;
import com.northpool.operator.type.TypeEnum;
import com.northpool.operator.utils.StpOperatorHelper;
import com.northpool.resources.datasource.IDataSource;
import com.northpool.resources.datatable.operate.ITableOperator;
import com.northpool.resources.datatable.operate.IndexBean;

import java.util.List;
import java.util.Map;

@OperatorInfo(
        ref = "IndexJudgeOperator",
        name = "索引信息",
        description = "判断字段索引是否存在，返回索引信息"
)
@OutputParams({
        @OutputParam(ref = "hasIndex", name = "存在索引", desc = "是否存在字段索引结果，true或false"),
        @OutputParam(ref = "isPk", name = "是否为主键索引", desc = "是否为为主键索引，true或false"),
        @OutputParam(ref = "isUnique", name = "是否为唯一索引", desc = "是否为为唯一索引，true或false"),
        @OutputParam(ref = "isSpatial", name = "是否为空间索引", desc = "是否为空间索引，true或false"),
})
public class IndexJudgeOperator extends StpOperatorHelper {

    @InputParams({
            @InputParam(ref = "datasource", name = "数据源", desc = "数据源ID", type = TypeEnum.DATASOURCE, required = true),
            @InputParam(ref = "tableName", name = "表名称", desc = "数据表名称，如: tb_test", type = TypeEnum.STRING, required = true),
            @InputParam(ref = "field", name = "字段名称", desc = "字段名称，如: dltb", type = TypeEnum.STRING, required = true),
    })
    public IndexJudgeOperator() {
    }

    public static void main(String[] args) {
        OperatorTestBuild.execute(IndexJudgeOperator.class, TestParamBuilder.aBuilder()
                .param("datasource", "test_bcg")
                .param("tableName", "public.bj_landuse")
                .param("field", "gid")
                .build());
    }

    @Override
    public void execute(ParamTool paramTool) throws Exception {
        String dataSourceId = paramTool.getStr("datasource");
        String dataSourceJson = DatabaseUtils.getDataSourceJson(dataSourceId);
        String tableName = paramTool.getStr("tableName");
        String field = paramTool.getStr("field");
        IDataSource ds = IDataSource.fromJSON(dataSourceJson);

        ITableOperator tableOperator = ds.getTableOperator(tableName);
        Map<String, IndexBean> indexMap = tableOperator.toTableSchemaBean().getIndexMap();
        boolean hasIndex = false;
        boolean isPk = false;
        boolean isUnique = false;
        boolean isSpatial = false;
        for (IndexBean value : indexMap.values()) {
            List<String> colnames = value.getColnames();
            if (colnames.contains(field)) {
                hasIndex = true;
                isSpatial = value.getSpatial();
                isUnique = value.getUnique();
                isPk = value.getIsPK();
                break;
            }

        }

        this.result("hasIndex", hasIndex);
        this.result("isSpatial", isSpatial);
        this.result("isUnique", isUnique);
        this.result("isPk", isPk);
    }
}
